import os
import shutil
from pathlib import Path

def cleanup_test_files():
    """清理测试文件"""
    print("=== 开始清理测试文件 ===")
    
    # 要删除的测试文件列表
    test_files = [
        "test_image.jpg",
        "test_image_0.jpg", 
        "test_image_1.jpg",
        "test_image_2.jpg",
        "test_image_3.jpg",
        "test_image_4.jpg",
        "test_image_5.jpg",
        "test_image_6.jpg",
        "test_image_7.jpg",
        "test_image_8.jpg",
        "test_image_9.jpg",
        "test_upload.py",
        "test_upload_no_auth.py",
        "test_upload_full_url.py",
        "test_upload_api.py",
        "test_collation_fix.py",
        "fix_database_collation.py",
        "test_fix_verification.py",
        "quick_fix.py",
        "quick_fix_final.py",
        "check_database_structure.py",
        "compare_project_fields.py",
        "fix_field_errors.py",
        "field_mapping.py",
        "auto_fix_sql.py",
        "immediate_fix.py",
        "fix_database_once.py",
        "quick_fix_methods.py",
        "fix_database_final.py",
        "test_goods_query.py",
        "update_repositories.py",
        "scan_database_connections.py",
        "update_all_repositories.py",
        "check_goods_table.py",
        "test_db_connection.py",
        "test_user_api.py",
        "check_goods_data.py",
        "check_specs_data.py",
        "check_specs_fields.py",
        "check_specs_structure.py",
        "check_table_structure.py",
        "check_users_table.py",
        "create_sample_specs.py",
        "extract_database_connections.py",
        "fix_specs_table.py",
        "quick_fix_final_v2.py",
        "simple_fix_specs.py"
    ]
    
    # 要删除的测试目录
    test_dirs = [
        "test_output",
        "temp_files",
        "debug_logs"
    ]
    
    deleted_files = []
    deleted_dirs = []
    
    # 删除测试文件
    for file_name in test_files:
        if os.path.exists(file_name):
            try:
                os.remove(file_name)
                deleted_files.append(file_name)
                print(f"✅ 删除文件: {file_name}")
            except Exception as e:
                print(f"❌ 删除文件失败 {file_name}: {e}")
    
    # 删除测试目录
    for dir_name in test_dirs:
        if os.path.exists(dir_name):
            try:
                shutil.rmtree(dir_name)
                deleted_dirs.append(dir_name)
                print(f"✅ 删除目录: {dir_name}")
            except Exception as e:
                print(f"❌ 删除目录失败 {dir_name}: {e}")
    
    # 清理static/uploads目录中的测试文件（保留目录结构）
    upload_dir = "static/uploads"
    if os.path.exists(upload_dir):
        print(f"\n检查上传目录: {upload_dir}")
        for root, dirs, files in os.walk(upload_dir):
            for file in files:
                if file.startswith("test_") or "test" in file.lower():
                    file_path = os.path.join(root, file)
                    try:
                        os.remove(file_path)
                        deleted_files.append(file_path)
                        print(f"✅ 删除测试图片: {file_path}")
                    except Exception as e:
                        print(f"❌ 删除测试图片失败 {file_path}: {e}")
    
    print(f"\n=== 清理完成 ===")
    print(f"删除文件数量: {len(deleted_files)}")
    print(f"删除目录数量: {len(deleted_dirs)}")
    
    if deleted_files:
        print("\n删除的文件:")
        for file in deleted_files:
            print(f"  - {file}")
    
    if deleted_dirs:
        print("\n删除的目录:")
        for dir in deleted_dirs:
            print(f"  - {dir}")

if __name__ == "__main__":
    cleanup_test_files() 